package com.lzy.imagepickerdemo.zn.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DBManager {
    private static final int BUFFER_SIZE = 1024;
    private static final String ASSETS_NAME = "zn_photo.db";
    private static final String DB_NAME = "zn_photo.db";
    private String DB_PATH;
    private Context mContext;
    public DBManager(Context context) {
        this.mContext = context;
        DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/zn" + File.separator + "databases" + File.separator;
    }
    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void copyDBFile(){
        File dir = new File(DB_PATH);
        if (!dir.exists()){
            dir.mkdirs();
        }
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbFile.exists()){
            copy(dbFile);
        }else{
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
            String dbVersion="0";
            if (Integer.parseInt(dbVersion)>db.getVersion()){
                //进行数据库升级
                copy(dbFile);
            }
            db.close();
        }
    }

    private void copyPath( File dbFile,File path){
        InputStream is;
        OutputStream os;
        try {
            is = new FileInputStream(path);;
            os = new FileOutputStream(dbFile);
            byte[] buffer = new byte[BUFFER_SIZE];
            int length;
            while ((length = is.read(buffer, 0, buffer.length)) > 0){
                os.write(buffer, 0, length);
            }
            os.flush();
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void copy( File dbFile){
        InputStream is;
        OutputStream os;
        try {
            is = mContext.getResources().getAssets().open(ASSETS_NAME);
            os = new FileOutputStream(dbFile);
            byte[] buffer = new byte[BUFFER_SIZE];
            int length;
            while ((length = is.read(buffer, 0, buffer.length)) > 0){
                os.write(buffer, 0, length);
            }
            os.flush();
            os.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取相片类型
     * @return
     */
    public List<PhotoTypeEntity> getPhotoType(){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        Cursor cursor = db.rawQuery("select * from " + "photo_type", null);
        List<PhotoTypeEntity> mListMoPhoneBrand=new ArrayList<>();
        while (cursor.moveToNext()){
            int photo_type = cursor.getInt(cursor.getColumnIndex("photo_type"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            mListMoPhoneBrand.add(new PhotoTypeEntity(photo_type,name));
        }
        cursor.close();
        db.close();
        return mListMoPhoneBrand;
    }
    public List<PhotoTypeEntity> getPhotoType(String photo_name){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        Cursor cursor = db.rawQuery("select * from " + "photo_type"+" where name LIKE '"+photo_name+"%'", null);
        List<PhotoTypeEntity> mListMoPhoneBrand=new ArrayList<>();
        while (cursor.moveToNext()){
            int photo_type = cursor.getInt(cursor.getColumnIndex("photo_type"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            mListMoPhoneBrand.add(new PhotoTypeEntity(photo_type,name));
        }
        cursor.close();
        db.close();
        return mListMoPhoneBrand;
    }
    /**
     * 添加相片类型
     * @param ed
     */
    public void addPhotoType(int photo_type,String ed){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        ContentValues values = new ContentValues();

        values.put("photo_type",photo_type);
        values.put("name",ed);
        db.insert("photo_type", null, values);
        db.close();
    }

    /**
     * 删除相册
     * @param photo_type
     */
    public void delPhotoType(int photo_type){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.delete("photo_type", "photo_type='"+photo_type+"'", null);
        db.close();

    }
    /**
     * 获取全部相片
     * @return
     */
    public List<PhotoListEntity> getPhotolist(int postion) throws Exception{
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        Cursor cursor = db.rawQuery("select * from " + "photo_list"+" where photo_type="+postion, null);
        List<PhotoListEntity> mListMoPhoneBrand=new ArrayList<>();
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            int photo_type = cursor.getInt(cursor.getColumnIndex("photo_type"));
            int img_id = cursor.getInt(cursor.getColumnIndex("img_id"));
            String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
            String img_name = cursor.getString(cursor.getColumnIndex("img_name"));
            mListMoPhoneBrand.add(new PhotoListEntity(id,photo_type,img_id,img_url,img_name));
        }
        cursor.close();
        db.close();
        return mListMoPhoneBrand;
    }

    /**
     * 添加记录
     * @param values
     */
    public void addPhotoList(  ContentValues values){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.insert("photo_list", null, values);
        db.close();
    }
 /*   public boolean addPhoto(PhotoListEntity mPhotoListEntity){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        ContentValues values = new ContentValues();
        values.put("id",id);
        values.put("name",name);
        values.put("phone", phone);
        values.put("pwd", pwd);
        db.insert(admin_user, null, values);
        cursor.close();
        db.close();
        return true;
    }

    *//**
     * 删除用户
     * @param name
     * @return
     *//*
    public boolean delUser(String name){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        db.delete(admin_user, "name='"+name+"'", null);
        db.close();
        return true;
    }
*/
}
